package com.itheima.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.common.R;
import com.itheima.dto.SetmealDto;
import com.itheima.entity.Category;
import com.itheima.entity.Setmeal;
import com.itheima.service.CategoryService;
import com.itheima.service.impl.SetmealServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@RestController
@Slf4j
@RequestMapping("/setmeal")
public class SetmealController {
    @Autowired
    private SetmealServiceImpl setmealServicel;
    @Autowired
    private CategoryService categoryService;
    //新增套餐
@PostMapping
@CacheEvict(value = "setmealCache",allEntries = true)
    public R<String> add(@RequestBody SetmealDto setmealDto){
   setmealServicel.saves (setmealDto);
   return R.success ("新增套餐成功");
}
@GetMapping("/page")
//菜品表的分页构造-》拷贝到dto的分页构造器（除了page下的records）-》
// 获得dish的records拷贝到dto-》获得分类id-》id查询分类对象-》
// 分类对象存入dto分页构造器的records
public R<Page> page(int page,int pageSize,String name){
    Page<Setmeal> pageInfo=new Page<> (page,pageSize);
    Page<SetmealDto> mealPage=new Page<> ();
    LambdaQueryWrapper<Setmeal> qw=new LambdaQueryWrapper<> ();
    qw.eq (name!=null,Setmeal::getName,name);
    qw.orderByDesc (Setmeal::getUpdateTime);
    setmealServicel.page (pageInfo,qw);
    BeanUtils.copyProperties (pageInfo,mealPage,"records");
    List<Setmeal> records=pageInfo.getRecords ();

    List<SetmealDto> list = records.stream().map((item) -> {
        SetmealDto setmealDto = new SetmealDto();
        //对象拷贝
        BeanUtils.copyProperties(item,setmealDto);
        //分类id
        Long categoryId = item.getCategoryId();
        //根据分类id查询分类对象
        Category category = categoryService.getById(categoryId);
        if(category != null){
            //分类名称
            String categoryName = category.getName();
            setmealDto.setCategoryName(categoryName);
        }
        return setmealDto;
    }).collect(Collectors.toList());

    mealPage.setRecords(list);

    return R.success(mealPage);
}
//删除
    @DeleteMapping
    //清除setmealCache名称下,所有的缓存数据
    @CacheEvict(value = "setmealCache",allEntries = true)
    public R<String> deletes(@RequestParam List<Long> ids){
    setmealServicel.remove (ids);
    return R.success ("删除成功");
    }
    //售卖状态
    @PostMapping("/status/{status}")
    public R<String> status(@PathVariable Integer status,@RequestParam List<Long> ids){
        LambdaQueryWrapper<Setmeal> qw=new LambdaQueryWrapper<> ();
        qw.in(Setmeal::getId,ids);
        List<Setmeal> list=setmealServicel.list (qw);
        for (Setmeal setmeal : list) {
            if(setmeal!=null){
                setmeal.setStatus (status);
                setmealServicel.updateById (setmeal);
            }
        }
        return R.success ("状态修改成功");
    }
    @Cacheable(value = "setmealCache",key = "#setmeal.categoryId + '_' + #setmeal.status")
    @GetMapping("/list")
    public R<List<Setmeal>>list(Setmeal setmeal){
        LambdaQueryWrapper<Setmeal> qw=new LambdaQueryWrapper<> ();
        qw.eq (Setmeal::getCategoryId, setmeal.getCategoryId ());
        qw.eq (Setmeal::getStatus, setmeal.getStatus ());
        qw.orderByDesc (Setmeal::getUpdateTime);
        List<Setmeal> list=setmealServicel.list (qw);
        return R.success (list);
    }
}
